package cn.hdmis.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.struts2.json.annotations.JSON;

import cn.hdmis.bean.QueryResult;
import cn.hdmis.entity.Teacher;
import cn.hdmis.service.TeacherService;

public class TeacherAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private Teacher teacher;
	private List<Teacher> teacherList;
	private TeacherService teacherService;

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Teacher getTeacher() {
		return teacher;
	}
	
	public void setTeacherList(List<Teacher> teacherList) {
		this.teacherList = teacherList;
	}
	@JSON(name="rows")
	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	@JSON(serialize=false)
	public TeacherService getTeacherService() {
		return teacherService;
	}

	public String addTeacher(){
		try {
			teacher.setNoCode(String.valueOf(new Random().nextInt(9999)+1000));
			teacherService.addTeacher(teacher);
		} catch (Exception e) {
			setMSG(MSG_FAILED);
			logger.error(e.getMessage());
		}
		return "addSuccess";
	}
	
	@SuppressWarnings("unchecked")
	public String  loadAllTeacher(){
		QueryResult result  = null;
		initPage();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(teacher!=null){
			paramMap.put("id",teacher.getId());
			paramMap.put("name", teacher.getName());
		}
		try {
			result = teacherService.getAllTeacher(pageBean.getFirstindex(),pageBean.getPageSize(),paramMap);
			this.setTeacherList(result.getResultList());
			pageBean.setTotalCount(result.getTotalRecord());
		} catch (Exception e) {
			logger.error(e.getMessage());
			setMSG(MSG_FAILED);
		}
		return "teacherList";
	}
	
	public String view(){
		String userId = this.getRequest().getParameter("id");
		if(userId!=null&&!userId.trim().isEmpty()){
			try {
				Teacher teacher = teacherService.getTeacher(Integer.valueOf(userId));
				this.setTeacher(teacher);
			} catch (Exception e) {
				logger.error(e.getMessage());
				setMSG(MSG_FAILED);
			}
		}
		
		return "view";
	}
	
	public String remove(){
		String userId = this.getRequest().getParameter("id");
		Integer id = Integer.valueOf(userId);
		try {
			this.teacherService.deleteTeacher(id);
		} catch (Exception e) {
			setMSG(MSG_FAILED);
		}
		return "remove";
	}
	
	public String removeMore(){
		String userId = this.getRequest().getParameter("ids");
		String[] idArry =  userId.split(",");
		List<Serializable> idList = new ArrayList<Serializable>();
		if(idArry.length>0){
			for (String id : idArry) {
				idList.add(Integer.valueOf(id));
			}
		}
		try {
			this.teacherService.deleteTeacherList(idList);
		} catch (Exception e) {
			logger.error(e.getMessage());
			setMSG(MSG_FAILED);
		}
		return "remove";
	}
}
